package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Answer;
import cn.wolfcode.trip.base.mapper.AnswerMapper;
import cn.wolfcode.trip.base.mapper.QuestionMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IAnswerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AnswerServiceImpl implements IAnswerService {
    @Autowired
    private AnswerMapper answerMapper;
    @Autowired
    private QuestionMapper questionMapper;

    @Override
    public void savaOrUpdate(Answer answer,Long questinoId) {
        if (answer.getId() != null) {
            answerMapper.updateByPrimaryKey(answer);
        } else {
            answerMapper.insert(answer);
            answerMapper.insertRelation(questinoId,answer.getId());
        }

    }

    @Override
    public void delete(Long id) {
        if (id != null) {
            answerMapper.deleteByPrimaryKey(id);
        }
    }

    @Override
    public List<Answer> listAll() {
        return answerMapper.selectAll();
    }

    @Override
    public List<Answer> getAnswerByQuestionId(Long questionId) {
        return answerMapper.selectAnswerByQuestionId(questionId);
    }

    @Override
    public Answer get(Long answerId) {
        return answerMapper.selectByPrimaryKey(answerId);
    }

    @Override
    public List<Answer> queryForList(QueryObject qo) {
        return answerMapper.selectAnswersByQuestionId(qo);
    }
}
